//页面初始化
$(document).ready(function(){
	jqGridBuild();
	//查询按钮
	$("#searchForm").submit(function() {
		commonSearch("dataTable","searchForm");
	});
	//页面自适应
	$(window).bind('resize', function() {
		var width = $('.jqGrid_wrapper').width();
		$('#dataTable').setGridWidth(width);
	});
});

//表格初始化
function jqGridBuild(){
	var defaultColNames =['角色名','备注','编号'];
	var defaultColModel =[ 
		{name : 'actorName',index :'ACTOR_NAME',width : 150}, 
		{name : 'summary',index : 'SUMMARY',width : 250}, 
		{name : 'id',index : 'ID',hidden:true}
	];
	//数据表格显示
	$("#dataTable").jqGridShow({
		caption: "角色列表",
		url : contextPath+'/actor/queryActorList.do',
		postData : getSearchFormJson("searchForm"),
		colNames : defaultColNames,
		colModel : defaultColModel,
		pager : "#tablePager",
        viewrecords: true,
        hidegrid: false,
        multiselect: true,
        rownumbers: false,
        shrinkToFit: false,
        autoScroll: true,
        height: 360,
        subGrid:false,//子表格显示
		serializeGridData: function(postData) {
		    return JSON.stringify(postData);
		},
        gridComplete: function () {
        	// 在此事件中循环为每一行添加修改和删除链接
            var ids = $("#dataTable").jqGrid('getDataIDs');
            for (var i = 0; i < ids.length; i++) {
                var id = ids[i];
                var model = $("#dataTable").jqGrid('getRowData',id);
                var operaHTML = getModifyBtn(model);
                // 这里的onclick就是调用了上面的javascript函数
                // Modify(id)
                $("#dataTable").jqGrid('setRowData', ids[i], {opera: operaHTML});
            }
        },
        ondblClickRow: function (id) {
        	console.log("双击事件，获取ID："+id);
        	return null;
        }
	});
	//添加按钮
	$("#dataTable").jqGrid('navGrid', '#tablePager', {
		edit: true,
		add: true,
		del: false,
		search: false,
		addfunc: function(){	
			add();
		}, 
		editfunc: function(rowId){
			edit();
		},
		delfunc: function(rowId){
		},
		searchfunc: function(){
			layer.open({
				type: 1,
				title: '查询条件',
				skin: 'layui-layer-lan',
				fixed:true,
				area: ['700px', '450px'],
				content: $('#searchCondition'),
				end: function(){
				}
			});
		},
		height: 200,
		reloadAfterSubmit: true
	});
}

//获取操作单元格HTML
function getModifyBtn(model){
	var operaHTML = "";
//	if(model.status == "初始化"){
//		operaHTML = operaHTML + "<a title='冻结' class='green previewInfo' href='#' onclick='freeznRepoTask(\""+model.repoTaskCode+"\")'>冻结数据</a>&nbsp; ";
//	} else if(model.status == "上报失败"){
//		operaHTML = operaHTML + "<a title='解冻数据' class='green previewInfo' href='#' onclick='unFreeznRepoTask(\""+model.repoTaskCode+"\")'>解冻数据</a>&nbsp; ";
//	} 
	return operaHTML;
}

//添加
function add() {
	layer.open({
	  type: 2,
	  title: '新增角色信息',
	  skin: 'layui-layer-lan',
	  area: ['100%', '100%'],
	  content: contextPath+'/actor/toUpdateActor.do',
	  maxmin: true
	});
}
//编辑
function edit() {
	var rows = $('#dataTable').jqGrid('getGridParam','selarrrow');
	if(rows && rows.length == 1){
		var actor = $('#dataTable').jqGrid('getRowData',rows[0]);
		layer.open({
		  type: 2,
		  title: '修改角色信息',
		  skin: 'layui-layer-lan',
		  area: ['100%', '100%'],
		  content: contextPath+'/actor/toUpdateActor.do?actorId=' + actor.id,
		  maxmin: true
		});
	} else {
		layer.alert('请选择记录!');
		return false;
	}
}
